package Main.center;

import Main.pojo.TcpMessage;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * PACKAGE_NAME: Main.tcp
 * Description:
 * TCP 保证稳定的信息传输，UDP保证文件P2P传输
 */
public class Center{
    public static void main(String[] args) throws IOException {
        Thread tcpThread = new Thread(new TCPService(9999));
        tcpThread.start();
        Thread udpThread = new Thread(new UDPService(9999));
        udpThread.start();
    }
}
class UDPService implements Runnable {
    private int udpPort = 9999;
    private DatagramSocket datagramSocket;

    public UDPService(int udpPort) throws SocketException {
        this.udpPort = udpPort;
        this.datagramSocket = new DatagramSocket(udpPort);
    }

    @Override
    public void run() {

    }
}
class TCPService implements Runnable {
    private int tcpPort = 9999;
    private ServerSocket serverSocket;
    // Map<sessionId, >
    private Map<String, ClientSession> clientSessionMap;

    public TCPService(int tcpPort) throws IOException {
        this.tcpPort = tcpPort;
        this.serverSocket = new ServerSocket(tcpPort);
        this.clientSessionMap = new HashMap<>();
    }

    @Override
    public void run() {
        while (true) {
            try {
                // 未收到信息前均为阻塞，建立连接后获得一个Socket
                Socket socket = this.serverSocket.accept();
                // 解析消息
                // 获取对象输入流
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                // 获取对象
                TcpMessage tcpMessage = (TcpMessage) objectInputStream.readObject();
                // 开始处理
                // 判断请求类型 1、请求注册加入 2、请求协助连接某主机
                switch (tcpMessage.code) {
                    case 1: // 1，null, String:clientName
                        // 创建sessionId
                        String newSessionId = UUID.randomUUID().toString();
                        // ClientSession
                        ClientSession newClientSession = new ClientSession();
                        newClientSession.sessionId = newSessionId;
                        newClientSession.clientIp = socket.getInetAddress().getHostAddress();
                        newClientSession.clientName = (String) tcpMessage.data;
                        newClientSession.expireTime = LocalDateTime.now();
                        // 30分钟失效
                        newClientSession.expireTime.plusMinutes(30);
                        this.clientSessionMap.put(newSessionId, newClientSession);
                        // 返回sessionId，返回状态为成功
                        TcpMessage returnMessage = new TcpMessage();
                        returnMessage.code = 1001; // 1001 连接成功
                        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                        objectOutputStream.writeObject(returnMessage);
                        objectOutputStream.flush();
                        break;
                }
                // 回复完成后关闭连接
                socket.close();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    class ClientSession {
        public String sessionId;
        public String clientName;
        public String clientIp;
        public LocalDateTime expireTime;
    }
}